package com.wy.ratingbar;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * Time:8/3/21
 * Author:ypf
 * Description:
 */
public class RatingBar extends View {

    private String TAG = "RatingBarLog";

    private Paint mBgPaint;

    private int mStartCount = 0;

    public RatingBar(Context context) {
        this(context, null, 0);
    }

    public RatingBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RatingBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mBgPaint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //五角星背景
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.wujiaoxingyuanjiao);
        int bitmapWidth = bitmap.getWidth();
        int bitmapHeigth = bitmap.getHeight();

        int width = getWidth();
        int height = getHeight();

        Log.i(TAG, "bitmapWidth:" + bitmapWidth + ",bitmapHeigth:" + bitmapHeigth);

        // 取得想要缩放的matrix参数.
        Matrix matrix = new Matrix();
        float scaleWidth = ((float) height) / bitmapWidth;
        float scaleHeight = ((float) height) / bitmapHeigth;
        Log.i(TAG, "scaleWidth:" + scaleWidth + ",scaleHeight:" + scaleHeight);
        matrix.postScale(scaleWidth, scaleHeight);

        // 得到新的图片.
        Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeigth, matrix, true);

        canvas.drawBitmap(newBitmap, 0, 0, mBgPaint);

        canvas.drawBitmap(newBitmap, height, 0, mBgPaint);

        canvas.drawBitmap(newBitmap, height * 2, 0, mBgPaint);

        canvas.drawBitmap(newBitmap, height * 3, 0, mBgPaint);

        canvas.drawBitmap(newBitmap, height * 4, 0, mBgPaint);

        //五角星前景
        Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.mipmap.wujiaoxingyuanjiao_2);
        Bitmap newBitmap2 = Bitmap.createBitmap(bitmap2, 0, 0, bitmapWidth, bitmapHeigth, matrix, true);

        for (int i = 0; i < mStartCount; i++) {
            canvas.drawBitmap(newBitmap2, height * i, 0, mBgPaint);
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        int height = getHeight();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        Log.i(TAG, "x:" + event.getX());
        float x = event.getX();
        mStartCount = (int) (x / height) + 1;
        Log.i(TAG, "mStartCount:" + mStartCount);
        invalidate();
        return true;
    }
}
